﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using PSE.Framework.ErrorLogging;
using PSE.Framework.Configuration;

namespace PSE.Updater.Common
{
    public class Util
    {
        private const string CONST_SYSTEM = "{0}Parameters_v";
        private const string CONST_FOLDER = "BMFBOVESPA";

        #region [Singleton]
        private Util() { }
        private static Util _instance;
        public static Util Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new Util();

                return _instance;
            }
        }
        #endregion [Singleton]

        public int ExecuteSystem(string file)
        {
            return ExecuteSystem(file, string.Empty, false);
        }

        public int ExecuteSystem(string file, string args)
        {
            return ExecuteSystem(file, args, false);
        }

        public int ExecuteSystem(string file, string args, bool waitForExit)
        {
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();

            if (string.IsNullOrEmpty(file))
                throw new ArgumentNullException("file");

            startInfo.FileName = file;

            if(!string.IsNullOrEmpty(args))
                startInfo.Arguments = args;

            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
            startInfo.CreateNoWindow = true;
            startInfo.UseShellExecute = false;

            System.Diagnostics.Process process = System.Diagnostics.Process.Start(startInfo);
            if (waitForExit)
            {
                process.WaitForExit();
                return process.ExitCode;
            }
            return 0;
        }

        public string GetInstallationPath(string systemName)
        {
            //string installationPath = string.Empty;
            ////string path = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
            ////string[] directories = System.IO.Directory.GetDirectories(System.IO.Path.Combine(path, CONST_FOLDER));
            ////foreach (string directory in directories)
            ////{
            ////    string[] files = System.IO.Directory.GetFiles(directory);
            ////    string[] folders = directory.Split('\\');
            ////    string systemRoot = folders[folders.Length - 1];
            ////    string system = string.Format(CONST_SYSTEM, systemRoot);
            ////    foreach (var file in files)
            ////    {
            ////        string xmlFile = System.IO.Path.GetFileName(file);
            ////        if (xmlFile.StartsWith(system))
            ////        {
            ////            string version = xmlFile.Replace(system, string.Empty).Replace(".xml", string.Empty);
            ////            int majorVersion = int.Parse(version);

            ////            if (systemRoot.Equals(systemName, StringComparison.InvariantCultureIgnoreCase))
            ////                installationPath = GetInstallationPath(file, systemRoot);
            ////       }
            ////    }
            ////}
            //return installationPath;

            var installationPath = ApplicationConfiguration.GetApplicationRootDirectory(SystemComponents.GetSystem(systemName));
            return installationPath;
        }

        public string GetInstallationPath(string file, string systemName)
        {
            try
            {
                XmlDocument xml = new XmlDocument();
                xml.Load(file);
                string path = xml.ChildNodes.Item(1).Attributes["InstallationPath"].Value;

                return System.IO.Path.Combine(path, systemName);
            }
            catch (Exception ex)
            {
                ErrorLogger.WriteLog(LogType.Error, ex);
                //throw ex;
                return string.Empty; 
            }
        }
    }
}
